from logic.states import *
from logic.cards import translateCards, parseCardState, translateCombinationCode

__author__ = 'cjay'

debug = False

class StateMachine():

    def __init__(self):
        self.firstState = None
        self.currentState = None

        self.p_money = 400      # player`s money
        self.c_money = 400      # computer`s money

        self.min = 10
        self.max = 10

        self.p_put_money = 0
        self.dropped_money = 0
        self.c_put_money = 0

        self.c_make_step = False
        self.p_make_step = False

        self.bank_money = 0         # bank`s money at current lap
        self.p_cards = []           #player`s cards
        self.c_cards = []          # computer`s cards
        self.cards = []             # common cards

        self.player_first = False   # who first? player or computer?
        self.bets_count = 0         # how often the rate was increased

        self.blind = 10             # size of blind
        self.bigblind = 20
        self.hasBlind = False
        self.hasBigBlind = False

        self.p_check = False
        self.cpu_check = False

        self.is_allin = False       # player has no money but believe in his lucky

    def getPlayerCombination(self):
        cards = self.p_cards[::]
        cards.extend(self.cards)

        cards = translateCards(cards)
        parse = parseCardState(cards)
        print parse
        return translateCombinationCode(parse[0])

    def next(self):
        next_state = self.currentState.next()
        self.currentState = next_state
        self.currentState.activate()
        return next_state.name

    def cp_pay(self,size):
        print "CPU put money", size
        self.c_put_money+=size
        self.c_money-=size
        self.bank_money +=size

    def p_pay(self,size):
        print "put money", size
        self.p_put_money+=size
        self.p_money-=size
        self.bank_money +=size


def fillStateMachine(fsm):
    """
    Create all necessary states for this final state machine

    @type fsm: StateMachine
    @param fsm: final state machine

    >>> sm = StateMachine()
    >>> fillStateMachine(sm)
    True
    >>> sm.player_first = True
    >>> sm.currentState.name
    'start_state'
    >>> sm.next()
    'player_turn_state'
    >>> sm.next()
    'cp_turn_state'
    >>> sm.next()
    'player_turn_state'
    >>> sm.next()
    'cp_turn_state'
    >>> sm.next()
    'end_turn_state'

    The end of the first round

    >>> sm.next()
    'add_card_state'
    >>> sm.next()
    'cp_turn_state'
    >>> sm.next()
    'player_turn_state'
    >>> sm.next()
    'end_turn_state'

    The end of the second round

    'add_card_state'
    >>> sm.next()
    'cp_turn_state'
    >>> sm.next()
    'player_turn_state'
    >>> sm.next()
    'end_turn_state'

    The end of the third round

    'add_card_state'
    >>> sm.next()
    'cp_turn_state'
    >>> sm.next()
    'player_turn_state'
    >>> sm.next()
    'end_turn_state'

    The end of the fourth round

    'add_card_state'
    >>> sm.next()
    'cp_turn_state'
    >>> sm.next()
    'player_turn_state'
    >>> sm.next()
    'end_turn_state'

    The end of the fifth round

    >>> sm.next()
    'finish_state'
    >>sm.next()
    'loose_state'


    """
    start = StartState(fsm)
    player_turn = PlayerTurnState(fsm)
    cp_turn = CPTurnState(fsm)
    start.addState(player_turn)
    start.addState(cp_turn)
    end_turn = EndTurnState(fsm)
    win = WinState(fsm)
    loose = LooseState(fsm)
    player_turn.addState(end_turn)
    cp_turn.addState(end_turn)
    cp_turn.addState(win)
    player_turn.addState(cp_turn)
    player_turn.addState(loose)
    cp_turn.addState(player_turn)
    end_turn.addState(player_turn)
    end_turn.addState(cp_turn)

    finish = FinishState(fsm)
    end_turn.addState(finish)
    finish.addState(win)
    finish.addState(loose)

    fsm.firstState = start
    fsm.currentState = start
    fsm.currentState.activate()

    return True



if __name__=="__main__":
    debug = True
    fs = StateMachine()
    fillStateMachine(fs)
    fs.player_first = True
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()
    fs.next()





